import { action, observable } from 'mobx';
import { BaseViewModel, TextListener, speechApi } from 'orionos-eve-core';
import {bookList} from './bookData'
import { BASE_URL } from '../utils/config';

export class BookViewModel extends BaseViewModel{
    @observable public playing: boolean = false
    @observable public voiceOn: boolean = true
    @observable public infoText: string = ''
    @observable public books: any[] = []
    public constructor(){
        super('book')
    }
    public onStart() {
        this.setRecognization(true)
        this.hintSpeek()
        this.searchBook('')
    }
    public onStop() {
    }
    public onHidden() {
    }
    public setRecognization(isOpen: boolean){
        speechApi.setRecognizable(isOpen)
        speechApi.setRecognizeMode(isOpen)
    }
    public getVoiceOn(): boolean{
        return this.voiceOn
    }
    @action
    public setVoiceOn(voiceOn: boolean){
        this.setRecognization(voiceOn)
        this.voiceOn = voiceOn
    }
    public getPlayState(){
        return this.playing
    }
    @action
    public setPlayState(playing: boolean){
        this.playing = playing
    }
    public getInfoText(){
        return this.infoText
    }
    @action
    public setInfoText(text: string){
        this.infoText = text
    }
    public getBooks(){
        return this.books
    }
    @action
    public setBooks(books: any[]){
        this.books = books
    }
    public hintSpeek(){
        this.speak('你可以对我说我想查找某某图书，比如我想查找骆驼祥子')
    }
    public speak(answer: string){
        let listener = new TextListener()
        listener.setFinish(()=>{
            this.setPlayState(false)
            listener.removeListener()
        })
        this.playing = true
        speechApi.playText(listener.getId(), answer)
    }
    public searchBook(bookname: string){
        console.log('搜索图书：' + bookname)
        //调用第三方图书查询列表接口---待定！当前使用本地数据
        let bookArr = bookList.map((e: any) =>{
            e.coverImg = ''
            if(e.imagefileid){
                e.coverImg = `${BASE_URL}/FileApi/File/Download?fileid=${e.imagefileid}`
            }
            return e
        })
        this.setBooks(bookArr)
    }
    public toHome(){
        this._apiTrigger(1004, '')
    }
    public toDetail(bookid: any){
        this._apiTrigger(1007, bookid)
    }
}